package com.cnplan.controller;

import com.cnplan.core.KUtil;
import com.cnplan.entity.ArcRoll;
import com.cnplan.service.ArcFileService;
import com.cnplan.service.ArcRollService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/arc_roll")
public class ArcRollController {

    @Autowired
    private ArcRollService arcRollService;
    @Autowired
    private  KUtil kUtil;
    @Autowired
    private ArcFileService arcFileService;
    private String urlport="http://192.168.6.90:8080";
    private static String imgPath="src\\main\\webapp\\WEB-INF\\static\\img\\test";
    @GetMapping("/findpage")
    public Object findPage(@RequestParam Map map){
        return arcRollService.findByArcRoll(map);
    }

    @GetMapping("/listAll")
    @ResponseBody
    public List<ArcRoll> archivesList(@RequestParam Map map, Model model){
        List<ArcRoll> arcRollList=arcRollService.findByArcRollList(map);
        return arcRollList;
    }

    /**
     * 测试使用，看是否可以删除缓存图片
     * */
    @GetMapping("/imagebase64")
    @ResponseBody
    public void imagebase64(){
        kUtil.imageNum();
//        String imgPath=KUtil.class.getClassLoader().getResource("/").getPath().replace("classes", "static")+"/img/test";
//        KUtil.deleteDir(new File(imgPath+"/tif"));
    }

    /**
     * 打印
     * */
    @GetMapping("/batchprint")
    @ResponseBody
    public void batchPrint(@RequestParam Map map){
        arcRollService.batchPrinting(map);
    }

    @GetMapping("/getImg")
    public void getImg(HttpServletResponse ree, @RequestParam String url){
        try {
            URL readUrl=new URL(urlport+url);
            URLConnection urlConnection=readUrl.openConnection();
            urlConnection.setConnectTimeout(3000);
            urlConnection.setReadTimeout(5000);
            urlConnection.connect();
            InputStream inputStream=urlConnection.getInputStream();
            BufferedImage img=ImageIO.read(inputStream);
            if(img!=null){
                String format=url.substring(url.lastIndexOf(".")+1);
                ImageIO.write(img,format,ree.getOutputStream());
            }
        }catch (Exception eee){
            eee.printStackTrace();
        }

    }

}
